package com.ricecoder.spring01.controller;

import com.ricecoder.spring01.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

//添加controller控制器注解
@Controller
public class UserController {

//自动注入实现类
    @Autowired
    UserService userService;

//    根目录请求路由
    @RequestMapping("/")
    public String index(Model model){
        model.addAttribute("data","123");
        return "hello";
    }

//    localhost:port/user请求路由，传递了data为所有用户信息对象
    @RequestMapping("/user")
    public String getUser(Model model){
        model.addAttribute("data",userService.allUsers());
        return "user";
    }

//    localhost:port/findUser请求路由，测试指定了查询用户姓名的运行结果
    @RequestMapping("/findUser")
    @ResponseBody
    public Map<String, Object> getUserByUserName(Model model){
        return userService.getUserByUserName("caozhifeng");
    }
}
